<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-item">
        <span>分类名称:</span>
        <el-input v-model="listQuery.name" placeholder="请输入分类名称" style="width: 200px" />
      </div>
      <div class="filter-item">
        <span>所属数据源id:</span>
        <el-select v-model="listQuery.sourceId" clearable placeholder="请选择所属数据源id" style="width: 200px">
          <el-option v-for="item in dictList('')" :key="'type' + item.label" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <div class="filter-item">
        <span>所属表id:</span>
        <el-select v-model="listQuery.tableId" clearable placeholder="请选择所属表id" style="width: 200px">
          <el-option v-for="item in dictList('')" :key="'type' + item.label" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <div class="filter-item">
        <span>所属字段id:</span>
        <el-select v-model="listQuery.columnId" clearable placeholder="请选择所属字段id" style="width: 200px">
          <el-option v-for="item in dictList('')" :key="'type' + item.label" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button v-permission="['directory:metadataDirectory:add']" class="filter-item" style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">
        {{ $t('table.add') }}
      </el-button>
    </div>

    <el-table :data="list" style="width: 100%" row-key="id" border lazy :load="load">
      <el-table-column label="分类id" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="父分类id" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.parentId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类名称" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="层级" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.level }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.sort }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属数据源id" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.sourceId | dictLabel('') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属表id" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.tableId | dictLabel('') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属字段id" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.columnId | dictLabel('') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" min-width="230" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button v-permission="['directory:metadataDirectory:update']" type="text" size="small" icon="el-icon-edit" @click="handleUpdate(row)">
            {{ $t('table.edit') }}
          </el-button>
          <el-button v-permission="['directory:metadataDirectory:delete']" type="text" size="small" icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(row)">
            {{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <metadata-directory-form ref="form" @refreshList="getList" />
  </div>
</template>

<script>
import { deletemetadataDirectory, fetchmetadataDirectoryList } from '@/api/directory/metadataDirectory'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import metadataDirectoryForm from './metadataDirectoryForm'

export default {
  name: 'metadataDirectory',
  components: { metadataDirectoryForm },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: [],
      listLoading: true,
      listQuery: {
        name: undefined,
        sourceId: undefined,
        tableId: undefined,
        columnId: undefined,
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      this.list = []
      fetchmetadataDirectoryList(this.listQuery).then((response) => {
        if (response.data.code === 0) {
          this.list = response.data.data
          this.$refs.form.setList(this.list)
        } else {
          this.$message.error(response.data.msg)
        }
        this.listLoading = false
      })
    },
    load(tree, treeNode, resolve) {
      resolve(tree.children)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleUpdate(row) {
      this.$refs.form.setList(this.list)
      this.$refs.form.handleUpdate(row.id)
    },
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deletemetadataDirectory(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleCreate() {
      this.$refs.form.setList(this.list)
      this.$refs.form.handleCreate()
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}
</style>
